<script setup lang="ts">
import { onBeforeUnmount } from 'vue';

const props: any = defineProps<{
    ujt: any;
}>();

const { $global, $fn } = props.ujt;

if (!$global.msg) {
    $global.msg = {
        tm: null,
        data: [],
        bak: [],
        fn: msgfn,
    };
}
function msgfn(item: any) {
    let reTmp: any = null;
    if (item === 1) {
        if ($global?.msg?.data?.length) {
            reTmp = $global.msg.data.shift();
            if (reTmp?.tmout) {
                clearTimeout(reTmp.tmout);
                delete reTmp.tmout;
            }
            $global.msg.bak.push(reTmp);
            if ($global.msg.bak.length > 100) {
                $global.msg.bak.splice(80);
            }
        }
    } else {
        if (
            !$global?.msg?.data?.length ||
            !$global?.msg?.data?.find(
                (el: any) => el.con === item.con && el.tit === item.tit
            )
        ) {
            item.tm = $fn.As.getTime();
            $global.msg.data.push(item);
            item.tmout = setTimeout(() => {
                $global.msg.fn(1);
            }, item?.time || 3000);
        }
    }
    if (reTmp) {
        return reTmp;
    }
}
function clearMsg() {
    let msglen: number = $global.msg.data.length;
    while (msglen--) {
        msgfn(1);
    }
}
onBeforeUnmount(() => {
    clearMsg();
});
</script>

<template>
    <div
        class="as-pop-msg-area"
        v-if="$global.msg.data?.length"
        :style="$global.msg.data?.maskstyle || {}"
        @pointerdown="clearMsg()"
        @touchstart="$fn.As.eStop"
    >
        <div class="as-pop-msg-list">
            <div class="as-pop-msg-li" v-for="item in $global.msg.data">
                <div :class="'as-pop-msg' + (item?.type || '')">
                    <div class="as-pop-msg-tit" v-if="item?.tit">
                        {{ item.tit }}
                    </div>
                    <div class="as-pop-msg-con" v-if="item?.con">
                        {{ item.con }}
                    </div>
                    <div class="as-pop-msg-tm" v-if="item?.tm">
                        [{{ item.tm }}]
                    </div>
                </div>
            </div>
        </div>
        <div class="as-pop-msg-btn" :title="$global?.lang?.asai?.clear">×</div>
    </div>
</template>

<style scoped lang="scss">
.as-pop-msg-area {
    user-select: none;
    cursor: not-allowed;
    position: fixed;
    z-index: 102;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    pointer-events: none;
    flex-direction: column;
    opacity: 0.9;
    .as-pop-msg-btn {
        display: none;
        pointer-events: auto;
        cursor: pointer;
        background-color: var(--ch0);
        color: var(--ch8);
        margin: 8px;
        width: 38px;
        height: 38px;
        display: flex;
        justify-content: center;
        align-items: center;
        border-radius: 50%;
        box-shadow: var(--shadow2);
        opacity: 0.2;
        &:hover {
            opacity: 1;
        }
    }
    .as-pop-msg-list {
        pointer-events: auto;
        text-align: center;
        width: 100%;
        height: auto;
        max-height: calc(100% - 80px);
        overflow: auto;
        .as-pop-msg-li {
            display: block;
            text-align: center;
            margin: 8px;
        }
    }
    .as-pop-msgok,
    .as-pop-msgerr,
    .as-pop-msg {
        border-radius: 18px;
        box-shadow: var(--shadow2);
        display: inline-flex;
        justify-content: center;
        align-items: center;
        width: auto;
        height: auto;
        padding: 5px 18px;
    }
    .as-pop-msg {
        background-color: var(--ch0);
        color: var(--ch8);
    }
    .as-pop-msgok {
        background-color: var(--cx0);
        color: var(--cx8);
    }
    .as-pop-msgerr {
        background-color: var(--cz0);
        color: var(--cz8);
    }
    .as-pop-msg-tit {
        font-weight: bold;
        text-align: center;
    }
    .as-pop-msg-con {
        text-align: center;
    }
    .as-pop-msg-tm {
        font-size: x-small;
        text-align: center;
        opacity: 0.4;
    }
}
</style>
